package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RadioButton;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.YuanwangListAdapter;
import com.xgkj.diyiketang.data.DesireInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.YuanwangListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import android.widget.Button;

import java.util.List;

public class YuanwanglistActivity extends BaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener,PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener {

    private Context mContext;
    private RelativeLayout titleBar;
    private TextView back;
    private RadioButton mychuangjianBtn;
    private RadioButton myjianzhengBtn;
    private PullToRefreshListView yuanwangList;
    private RadioGroup radioGroup;
    private Button createBtn;
    private NoDataUtil noDataUtil;
    private YuanwangListAdapter mAdapter;
    private int currentPage = 1;
    private int size = 20;
    private int type = 0;
    private int yuanwangTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuanwanglist);
        mContext = this;
        initView();
        View view = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,view);
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        mychuangjianBtn = (RadioButton) findViewById(R.id.mychuangjian_btn);
        myjianzhengBtn = (RadioButton) findViewById(R.id.myjianzheng_btn);
        yuanwangList = (PullToRefreshListView) findViewById(R.id.yuanwang_list);
        yuanwangList.init(mContext,true);
        yuanwangList.setOnRefreshListener(this);
        yuanwangList.setOnLoadDataListener(this);
        mAdapter = new YuanwangListAdapter(mContext);
        yuanwangList.setAdapter(mAdapter,true);
        createBtn = (Button) findViewById(R.id.create_btn);
        createBtn.setOnClickListener(this);
        mychuangjianBtn.setChecked(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.create_btn:
                Intent create_intent = new Intent(mContext,CreateYuanwangActivity.class);
                startActivityForResult(create_intent,151);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 151 && resultCode == RESULT_OK){
            yuanwangList.doRefresh();
        }
    }

    private void getYuanwangList(){
        if (yuanwangTaskId != -1){
            return;
        }
        if (currentPage == 0){
            yuanwangList.setNoNextPage();
            return;
        }
        YuanwangListTask yuanwangTask = new YuanwangListTask(type,currentPage,size);
        yuanwangTask.addListener(mListener);
        yuanwangTaskId = getEatShowApp().getTaskManager().addTask(yuanwangTask);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            yuanwangTaskId = -1;

            if (data != null){
                if (currentPage ==1){
                    mAdapter.clearData();
                    yuanwangList.onRefreshComplete();
                    yuanwangList.enableLoadMore();
                }else {
                    yuanwangList.onLoadDataComplete(true);
                }

                List<DesireInfo> list = (List<DesireInfo>) data;
                if (list != null && list.size()>0){
                    mAdapter.updateData(list);
                }

                if (mAdapter != null && mAdapter.getCount()>0){
                    noDataUtil.ListViewNoEmpty(yuanwangList);
                }else {
                    noDataUtil.ListViewEmpty(yuanwangList,getString(R.string.xiguan_wo_zuoqi),getString(R.string.create_my_yuanwang),R.drawable.kongbai);
                }

                currentPage = result;
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            yuanwangTaskId = -1;
            if (currentPage == 1){
                yuanwangList.onRefreshComplete();
            }else {
                yuanwangList.onLoadDataComplete(false);
            }
        }
    };

    private int mCheckId;
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == mCheckId) return;
        mCheckId = checkedId;
        if (checkedId==R.id.mychuangjian_btn){
            type = 0;
            createBtn.setVisibility(View.VISIBLE);
        }else if (checkedId == R.id.myjianzheng_btn){
            type = 1;
            createBtn.setVisibility(View.GONE);
        }
        mAdapter.clearData();
        yuanwangList.doRefresh();
    }


    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getYuanwangList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getYuanwangList();
    }
}
